<?php

namespace App\Http\Controllers\Tags;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Tags;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;

class TagsController extends Controller
{
	protected $ret;

	public function __construct()
    {
    	$this->ret = [];
    }

	//20180712标签列表
    public function index(Request $request){
    	$tags = new Tags();
    	
    	$where= array();
    	if (Input::has('keyword')){
    		$keyword = Input::get('keyword');
    		if(is_numeric($keyword)){
    			$where['id'] = $keyword;
    		}else{
    			$where['title'] = $keyword;
    		}
		}
		if (Input::has('state')){
    		$where['state'] = Input::get('state');
		}

		$list = $tags->listinfo($where);

    	$this->ret['list'] = $list;
    	$this->ret['state']= Input::get('state');
    	$this->ret['keyword']= Input::get('keyword');
        //dd($this->ret);
        return view('tags.index', $this->ret);
    }

    //20180712 添加标签
    public function addTag(Request $request){
    	$tags = new Tags();
    	if($request->input("title")){
            $tags->seo_title = $request->input("seo_title");
            $tags->seo_description = $request->input("seo_description");
    		$tags->title = $request->input("title");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请填写标签标题'));
    	}

    	if($request->input("type")){
    		$tags->type = $request->input("type");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请选择类别'));
    	}
        if($request->input("cover_url")){
            $tags->cover_url = $request->input("cover_url");
        }
        $tags->state = $request->input("state");
        $tags->opertion_user_id = $request->user()->id;
    	if($tags->save()){
    		return json_encode(array("code"=>1, 'msg'=>'添加成功'));
        }
    }

    //20180712 编辑标签
    public function editTag($id){
    	$tags = new Tags();
    	$tag  = $tags->one($id);
    	//dd($tag);
    	$this->ret['tag'] = $tag[0];

    	return view('tags.edit', $this->ret);
    }

    public function updateTag(Request $request){
    	$tags = new Tags();
    	
    	if($request->input("id")){
    		$tags->id = $request->input("id");
    		$tag      = $tags::find($tags->id);
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'参数错误'));
    	}
    	if($request->input("title")){
            $tag->seo_title = $request->input("seo_title");
            $tag->seo_description = $request->input("seo_description");
    		$tag->title = $request->input("title");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请填写标签标题'));
    	}

    	if($request->input("type")){
    		$tag->type = $request->input("type");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请选择类别'));
    	}
        if($request->input("cover_url")){
            $tag->cover_url = $request->input("cover_url");
        }
    	$tag->state = $request->input("state");
    	$tag->opertion_user_id = $request->user()->id;

    	if($tag->save()){
			$this->getSessionInfo($request,'success','编辑成功');
    		return json_encode(array("code"=>1, 'msg'=>'添加成功'));
        }
    }

    public function updateTagState($id,$state){
    	$tags       = new Tags();
    	$tag        = $tags::find($id);
    	$tag->state = $state;

    	if($tag->save()){
    		return redirect('/tags/list');
        }
    }

    public function deleteTag($id){
    	$tags = new Tags();
    	if($tags->deleteone($id)){
    		return redirect('/tags/list');
    	}
    }
}